define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var $ = require('jquery');
	var util = require('util');

	var Right = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'url': 'blackMagic/updateRight',
				"layout": {
					"module": '@base/dialog.base',
					"mask": 1,
					"showFoot": true, // 显示底层的取消和完成按钮
					"size": 'l',
					"close": null,
					"silence": false // 点击按钮发消息
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function() {
			var el = this.getDOM();
			$('<div pub-name="mod" pub-mod="@mod/fieldEdit.base">').appendTo(el);
			this.buildFromTemplate({
				mod: {
					fields: [
						{text: LANG('操作'), type: 'radio', field: 'op', validate: true,
							defaultValue: 'append',
							items: [
								{
									id: 'append',
									name: LANG('追加')
								},
								{
									id: 'reset',
									name: LANG('重置为')
								},
								{
									id: 'splice',
									name: LANG('删除')
								}
							]
						}
						,{text: LANG('当前权限点数目'), type: 'textarea', field: 'old_right', style: "width: 600px;height:400px", readonly: true}
						,{text: LANG('权限点'), tip: LANG('以逗号为做分隔权限点'), type: 'input', field: 'new_right'}
					],
					isPop: true,
					Title: LANG('权限信息:')
				}
			});
		},
		setData: function(data)
		{
			this.$data = data;
			this.$.mod.set_field('old_right', data && data.rights || '');
			return this;
		},
		onDialogOk: function()
		{
			var self = this;
			var data = self.$.mod.getData();
			var c = self.getConfig();
			if(!data.new_right)
			{
				return pubjs.alert('请填写对应的权限点')
			}

			pubjs.confirm(LANG('确认要更新权限点吗'), function(res) {
				if(res) {
					pubjs.loading.show();
					pubjs.mc.send(c.url, {_id: self.$data._id, operate: data.op, value: data.new_right}, function(err, data) {
						pubjs.loading.hide();
						if(err)
						{
							pubjs.alert(err.message);
							return false;
						}

						self.hide();
					});
				}
			});

			return false;
		},
		onDialogCancel: function()
		{
			this.reset().hide();
		},
		reset: function()
		{
			this.$.mod.reset();
			return this;
		}
	});
	exports.right = Right;
});